<?php
//自动生成,请根据需要修改
namespace app\article\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;
/**
* @title 文章分类表接口
* @description 文章分类表说明
* @group 文章
*/
class Category extends Base{

	/**
	* @title 按条件删除记录
	* @description 按条件删除文章分类表的数据
	* @author 开发者（zzz）
	* @url /article/category/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:class_name type:VarChar require:1 default:0 other: desc:类名
	* @param name:sort type:Int require:1 default:0 other: desc:排序值
	* @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
	* @param name:nickname type:VarChar require:1 default:0 other: desc:名称
	* @param name:creater type:VarChar require:1 default:0 other: desc:创建者
	* @param name:updater type:VarChar require:1 default:0 other: desc:更新者
	* @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
	* @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
	*
	*
	* @return id:id
	* @return class_name:类名
	* @return sort:排序值
	* @return parent_id:父类id
	* @return nickname:名称
	* @return creater:创建者
	* @return updater:更新者
	* @return status:当前数据状态值
	* @return ext1:预备字段1
	*
	*/
	public function delete (){
		$input=input();
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['class_name'] = Request::post('class_name');
		$input['sort'] = Request::post('sort/d');
		$input['parent_id'] = Request::post('parent_id');
		$input['nickname'] = Request::post('nickname');
		$input['creater'] = Request::post('creater');
		$input['updater'] = Request::post('updater');
		$input['status'] = Request::post('status');
		$input['ext1'] = Request::post('ext1');

		// input结果返回
		$msg['data']['id'] = $input['id'];
		$msg['data']['class_name'] = $input['class_name'];
		$msg['data']['sort'] = $input['sort'];
		$msg['data']['parent_id'] = $input['parent_id'];
		$msg['data']['nickname'] = $input['nickname'];
		$msg['data']['creater'] = $input['creater'];
		$msg['data']['updater'] = $input['updater'];
		$msg['data']['status'] = $input['status'];
		$msg['data']['ext1'] = $input['ext1'];

		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['id','=',$input['id']],
			['class_name','=',$input['class_name']],
			['sort','=',$input['sort']],
			['parent_id','=',$input['parent_id']],
			['nickname','=',$input['nickname']],
			['creater','=',$input['creater']],
			['updater','=',$input['updater']],
			['status','=',$input['status']],
			['ext1','=',$input['ext1']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('category')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取单条数据
	* @description 根据ID获取文章分类表的指定记录
	* @author 开发者（zzz）
	* @url /article/category/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		$input=input();
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');

		// input结果返回
		$msg['data']['id'] = $input['id'];

		//模块 - 验证器
		$categoryValidate = new \app\article\validate\Category;
		$validate = $categoryValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $categoryValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('category')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新文章分类表指定记录的数据
	* @author 开发者（zzz）
	* @url /article/category/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:class_name type:VarChar require:1 default:0 other: desc:类名
	* @param name:sort type:Int require:1 default:0 other: desc:排序值
	* @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
	* @param name:nickname type:VarChar require:1 default:0 other: desc:名称
	* @param name:creater type:VarChar require:1 default:0 other: desc:创建者
	* @param name:updater type:VarChar require:1 default:0 other: desc:更新者
	* @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
	* @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
	*
	*
	* @return id:id
	* @return class_name:类名
	* @return sort:排序值
	* @return parent_id:父类id
	* @return nickname:名称
	* @return creater:创建者
	* @return updater:更新者
	* @return status:当前数据状态值
	* @return ext1:预备字段1
	*
	*/
	public function edit (){
		$input=input();
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['class_name'] = Request::post('class_name');
		$input['sort'] = Request::post('sort/d');
		$input['parent_id'] = Request::post('parent_id');
		$input['nickname'] = Request::post('nickname');
		$input['creater'] = Request::post('creater');
		$input['updater'] = Request::post('updater');
		$input['status'] = Request::post('status');
		$input['ext1'] = Request::post('ext1');

        $user =  getIdByToken($input['token']);//通过token获取用户id
        if(count($user->all())>0){
            $msg['data']['creater']  = $input['creater'] = $user->all()['0']['account_id'];
            $msg['data']['updater']  = $input['updater'] = $user->all()['0']['account_id'];
        }else{
            $msg['msg'] = '无法获取用户数据！';
            return $this->errorJson($msg);
        }

		// input结果返回
		$msg['data']['id'] = $input['id'];
		$msg['data']['class_name'] = $input['class_name'];
		$msg['data']['sort'] = $input['sort'];
		$msg['data']['parent_id'] = $input['parent_id'];
		$msg['data']['nickname'] = $input['nickname'];
		$msg['data']['creater'] = $input['creater'];
		$msg['data']['updater'] = $input['updater'];
		$msg['data']['status'] = $input['status'];
		$msg['data']['ext1'] = $input['ext1'];

		//模块 - 验证器
		$categoryValidate = new \app\article\validate\Category;
		$validate = $categoryValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $categoryValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'class_name'=>$input['class_name'],
			'sort'=>$input['sort'],
			'parent_id'=>$input['parent_id'],
			'nickname'=>$input['nickname'],
			'creater'=>$input['creater'],
			'updater'=>$input['updater'],
			'status'=>$input['status'],
			'ext1'=>$input['ext1'],
		];
		//获取数据
		$update_res = Db::name('category')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 插入记录
	* @description 插入文章分类表数据
	* @author 开发者（zzz）
	* @url /article/category/add
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:class_name type:VarChar require:1 default:0 other: desc:类名
	* @param name:sort type:Int require:1 default:0 other: desc:排序值
	* @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
	* @param name:nickname type:VarChar require:1 default:0 other: desc:名称
	* @param name:creater type:VarChar require:1 default:0 other: desc:创建者
	* @param name:updater type:VarChar require:1 default:0 other: desc:更新者
	* @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
	* @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
	*
	*
	* @return class_name:类名
	* @return sort:排序值
	* @return parent_id:父类id
	* @return nickname:名称
	* @return creater:创建者
	* @return updater:更新者
	* @return status:当前数据状态值
	* @return ext1:预备字段1
	*
	*/
	public function add (){
		$input=input();
		//模块 - 输入数据
		$input['class_name'] = Request::post('class_name');
		$input['sort'] = Request::post('sort/d');
		$input['parent_id'] = Request::post('parent_id');
		$input['nickname'] = Request::post('nickname');
		$input['creater'] = Request::post('creater');
		$input['updater'] = Request::post('updater');
		$input['status'] = '1';
		$input['ext1'] = Request::post('ext1');

        $user =  getIdByToken($input['token']);//通过token获取用户id
        if(count($user->all())>0){
            $msg['data']['creater']  = $input['creater'] = $user->all()['0']['account_id'];
            $msg['data']['updater']  = $input['updater'] = $user->all()['0']['account_id'];
        }else{
            $msg['msg'] = '无法获取用户数据！';
            return $this->errorJson($msg);
        }

		// input结果返回
		$msg['data']['class_name'] = $input['class_name'];
		$msg['data']['sort'] = $input['sort'];
		$msg['data']['parent_id'] = $input['parent_id'];
		$msg['data']['nickname'] = $input['nickname'];
		$msg['data']['creater'] = $input['creater'];
		$msg['data']['updater'] = $input['updater'];
		$msg['data']['status'] = $input['status'];
		$msg['data']['ext1'] = $input['ext1'];

		//模块 - 验证器
		$categoryValidate = new \app\article\validate\Category;
		$validate = $categoryValidate->scene('except_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $categoryValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 新增  ---
		//插入的数据
		$insert_data = [
			'class_name'=>$input['class_name'],
			'sort'=>$input['sort'],
			'parent_id'=>$input['parent_id'],
			'nickname'=>$input['nickname'],
			'creater'=>$input['creater'],
			'updater'=>$input['updater'],
			'status'=>$input['status'],
			'ext1'=>$input['ext1'],
		];
		//添加数据
		$insert_res = Db::name('category')->data($insert_data)->insert();
		if(!$insert_res){
			$msg['msg'] = '插入数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $insert_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（paginate）
	* @description 获取文章分类表的列表数据（分页）
	* @author 开发者（zzz）
	* @url /article/category/get_list
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:class_name type:VarChar require:1 default:0 other: desc:类名
	* @param name:sort type:Int require:1 default:0 other: desc:排序值
	* @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
	* @param name:nickname type:VarChar require:1 default:0 other: desc:名称
	* @param name:creater type:VarChar require:1 default:0 other: desc:创建者
	* @param name:updater type:VarChar require:1 default:0 other: desc:更新者
	* @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
	* @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
	*
	* @param name:page type:Int require:0 default:0 other: desc:当前页
	* @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
	*
	* @return id:id
	* @return class_name:类名
	* @return sort:排序值
	* @return parent_id:父类id
	* @return nickname:名称
	* @return creater:创建者
	* @return updater:更新者
	* @return status:当前数据状态值
	* @return ext1:预备字段1
	*
	*/
	public function get_list (){
		$input=input();
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');
		$input['class_name'] = Request::param('class_name');
		$input['sort'] = Request::param('sort/d');
		$input['parent_id'] = Request::param('parent_id');
		$input['nickname'] = Request::param('nickname');
		$input['creater'] = Request::param('creater');
		$input['updater'] = Request::param('updater');
		$input['status'] = Request::param('status');
		$input['ext1'] = Request::param('ext1');

		// input结果返回
		$msg['data']['id'] = $input['id'];
		$msg['data']['class_name'] = $input['class_name'];
		$msg['data']['sort'] = $input['sort'];
		$msg['data']['parent_id'] = $input['parent_id'];
		$msg['data']['nickname'] = $input['nickname'];
		$msg['data']['creater'] = $input['creater'];
		$msg['data']['updater'] = $input['updater'];
		$msg['data']['status'] = $input['status'];
		$msg['data']['ext1'] = $input['ext1'];

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['class_name','like','%'.$input['class_name'].'%'],
			['sort','=',$input['sort']],
			['parent_id','like','%'.$input['parent_id'].'%'],
			['nickname','like','%'.$input['nickname'].'%'],
			['creater','like','%'.$input['creater'].'%'],
			['updater','like','%'.$input['updater'].'%'],
			['status','=',$input['status']],
			['ext1','like','%'.$input['ext1'].'%'],
		];
		//去除为空的条件
		$paginate_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($paginate_map,$v);
		}

		//获取数据
		$select_res = Db::name('category')->where($paginate_map)->order('sort','asc')->paginate();

		// paginate 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（select）
	* @description 获取文章分类表的列表数据（无分页）
	* @author 开发者（zzz）
	* @url /article/category/get_select
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:class_name type:VarChar require:1 default:0 other: desc:类名
	* @param name:sort type:Int require:1 default:0 other: desc:排序值
	* @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
	* @param name:nickname type:VarChar require:1 default:0 other: desc:名称
	* @param name:creater type:VarChar require:1 default:0 other: desc:创建者
	* @param name:updater type:VarChar require:1 default:0 other: desc:更新者
	* @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
	* @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
	*
	*
	* @return id:id
	* @return class_name:类名
	* @return sort:排序值
	* @return parent_id:父类id
	* @return nickname:名称
	* @return creater:创建者
	* @return updater:更新者
	* @return status:当前数据状态值
	* @return ext1:预备字段1
	*
	*/
	public function get_select (){
		$input=input();
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');
		$input['class_name'] = Request::param('class_name');
		$input['sort'] = Request::param('sort/d');
		$input['parent_id'] = Request::param('parent_id');
		$input['nickname'] = Request::param('nickname');
		$input['creater'] = Request::param('creater');
		$input['updater'] = Request::param('updater');
		$input['status'] = Request::param('status');
		$input['ext1'] = Request::param('ext1');

		// input结果返回
		$msg['data']['id'] = $input['id'];
		$msg['data']['class_name'] = $input['class_name'];
		$msg['data']['sort'] = $input['sort'];
		$msg['data']['parent_id'] = $input['parent_id'];
		$msg['data']['nickname'] = $input['nickname'];
		$msg['data']['creater'] = $input['creater'];
		$msg['data']['updater'] = $input['updater'];
		$msg['data']['status'] = $input['status'];
		$msg['data']['ext1'] = $input['ext1'];

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['class_name','like','%'.$input['class_name'].'%'],
			['sort','=',$input['sort']],
			['parent_id','like','%'.$input['parent_id'].'%'],
			['nickname','like','%'.$input['nickname'].'%'],
			['creater','like','%'.$input['creater'].'%'],
			['updater','like','%'.$input['updater'].'%'],
			['status','=',$input['status']],
			['ext1','like','%'.$input['ext1'].'%'],
		];
		//去除为空的条件
		$select_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($select_map,$v);
		}

		//获取数据
		$select_res = Db::name('category')->where($select_map)->select();

		// select 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
    /**
     * @title 获取列表数据（paginate）
     * @description 获取文章分类表的列表数据树状（分页）
     * @author 开发者（zzz）
     * @url /article/category/get_Tree_list
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:class_name type:VarChar require:1 default:0 other: desc:类名
     * @param name:sort type:Int require:1 default:0 other: desc:排序值
     * @param name:parent_id type:VarChar require:1 default:0 other: desc:父类id
     * @param name:nickname type:VarChar require:1 default:0 other: desc:名称
     * @param name:creater type:VarChar require:1 default:0 other: desc:创建者
     * @param name:updater type:VarChar require:1 default:0 other: desc:更新者
     * @param name:status type:TinyInt require:1 default:0 other: desc:当前数据状态值
     * @param name:ext1 type:VarChar require:1 default:0 other: desc:预备字段1
     *
     * @param name:page type:Int require:0 default:0 other: desc:当前页
     * @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
     *
     * @return id:id
     * @return class_name:类名
     * @return sort:排序值
     * @return parent_id:父类id
     * @return nickname:名称
     * @return creater:创建者
     * @return updater:更新者
     * @return status:当前数据状态值
     * @return ext1:预备字段1
     *
     */
    public function get_Tree_list (){
        $input=input();
        //模块 - 输入数据
        $input['id'] = Request::param('id/d');
        $input['class_name'] = Request::param('class_name');
        $input['sort'] = Request::param('sort/d');
        $input['parent_id'] = Request::param('parent_id');
        $input['nickname'] = Request::param('nickname');
        $input['creater'] = Request::param('creater');
        $input['updater'] = Request::param('updater');
        $input['status'] = Request::param('status');
        $input['ext1'] = Request::param('ext1');

        // input结果返回
        $msg['data']['id'] = $input['id'];
        $msg['data']['class_name'] = $input['class_name'];
        $msg['data']['sort'] = $input['sort'];
        $msg['data']['parent_id'] = $input['parent_id'];
        $msg['data']['nickname'] = $input['nickname'];
        $msg['data']['creater'] = $input['creater'];
        $msg['data']['updater'] = $input['updater'];
        $msg['data']['status'] = $input['status'];
        $msg['data']['ext1'] = $input['ext1'];

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['id','=',$input['id']],
            ['class_name','like','%'.$input['class_name'].'%'],
            ['sort','=',$input['sort']],
            ['parent_id','like','%'.$input['parent_id'].'%'],
            ['nickname','like','%'.$input['nickname'].'%'],
            ['creater','like','%'.$input['creater'].'%'],
            ['updater','like','%'.$input['updater'].'%'],
            ['status','=',$input['status']],
            ['ext1','like','%'.$input['ext1'].'%'],
        ];
        //去除为空的条件
        $paginate_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($paginate_map,$v);
        }

        //获取数据
        $select_res = Db::name('category')->where($paginate_map)->order('sort','asc') ->column('*');

       /* // paginate 返回数据
        $msg['list'] = $select_res;*/

        foreach ($select_res as $key=>$page) {
            $meta = [];
            if($page['title']){
                $meta['title']=$page['title'];
            }

            if($page['icon']){
                $meta['icon']=$page['icon'];
            }
            if($page['nocache']){
                $meta['noCache']=$page['nocache'];
            }
            if(!$select_res[$key]['component']){
                unset($select_res[$key]['component']);
            }
        }

        $pages_tree=$this->genTree($select_res);
        $msg['msg']='获取成功';
        $msg['data']=$pages_tree;
        return $this->successJson($msg);
        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * 将数据格式化成树形结构
     * @author Xuefen.Tong
     * @param array $items
     * @return array
     */
    function genTree($items) {
        $tree = array(); //格式化好的树
        foreach ($items as $item) {
            if(isset($items[$item['parent_id']])) {
                $items[$item['parent_id']]['children'][] = &$items[$item['id']];
            }else {
                $tree[] = &$items[$item['id']];
            }
        }
        return $tree;
    }

    /**
     * @title 移动分类
     * @description  移动分类
     * @author 开发者（郑佳杭）
     * @url /article/category/move_to
     * @method POST
     * @param name:son_id require:0 other: desc:要移动的菜单id
     * @param name:parent_id require:0 other: desc:父菜单id
     *
     * @return:
     *
     */
    public function move_to(){
        $data = input();
        $validate = new \think\Validate;
        $rule = [
            'son_id'  => 'require|number',
            'parent_id'  => 'require|number',

        ];

        $vmsg = [
            'son_id.require' => 'son_id必须',
            'son_id.number'   => 'son_id必须是数字',
            'parent_id.require' => 'parent_id必须',
            'parent_id.number'   => 'parent_id必须是数字',
        ];

        $result   = $validate::make($rule,$vmsg)
            ->batch()
            ->check($data);
        if ($result !== true) {
            $msg['msg']='校验失败';
            $msg['validate']=$validate->getError();
            $msg['error_id']='1-13-01-0013';
            return $this->errorJson($msg);
        }
        //$c_move_tree=new \app\article\model\Category();
       // $re=$c_move_tree->moveTo($data['son_id'],$data['parent_id']);

       $resul = Db::name('category')->where('id',$data['son_id'])->find();
       $resul['parent_id'] = $data['parent_id'];
        $s =Db::name('category')->where('id',$data['son_id'])->update($resul);
       if($s){
            $msg['msg']='修改成功';
            return $this->successJson($msg);
        }else{
            $msg['msg']='修改失败或已经移动到该节点';
            return $this->errorJson($msg);
        }
    }
}
